Public Class OrderConfirm
    Inherits System.Web.UI.Page

    Private Const ORDER_LIST_URL As String = "/Internal/OrderList.aspx"
    Private Const ORDER_INPUT_URL As String = "/Internal/OrderInput.aspx"
    Protected WithEvents lblOrderNoText As System.Web.UI.WebControls.Label
    Protected WithEvents dgItems As System.Web.UI.WebControls.DataGrid
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Public Const ORDER_INPUT_PAGEBO_SESSION_NAME As String = "MIMS_WRS.Web.OrderInput.PageBO"

#Region "   Properties"
    Protected Property PageBO() As COrder
        Get
            If IsNothing(Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME)) Then
                Throw New ApplicationException("Session expried. Please re-login")
            End If
            Return CType(Session(ORDER_INPUT_PAGEBO_SESSION_NAME), COrder)
        End Get
        Set(ByVal Value As COrder)
            Session(ORDER_INPUT_PAGEBO_SESSION_NAME) = Value
        End Set
    End Property
#End Region


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If CType(PageBO.OrderType, EOrderType) = EOrderType.Bulk_Order Then
            Me.lblOrderNoText.Text = "Customer BO#"
        Else
            Me.lblOrderNoText.Text = "Customer PO#"
        End If

        'Display all product items of this order.
        Me.dgItems.DataSource = PageBO.OrderItems
        Me.dgItems.DataBind()

    End Sub

    Protected Function GetRequestedETAText() As String
        Select Case CType(PageBO.RequestedETA, ERequestedETAType)
            Case ERequestedETAType.As_soon_as_possible
                Return CType(PageBO.RequestedETA, ERequestedETAType).ToString.Replace("_", " ")
            Case ERequestedETAType.After, ERequestedETAType.Before
                Return String.Format("{0} {1}", CType(PageBO.RequestedETA, ERequestedETAType).ToString, FormatDate(PageBO.ETADate1))
            Case ERequestedETAType.Between
                Return String.Format("Between {0} and {1}", FormatDate(PageBO.ETADate1), FormatDate(PageBO.ETADate2))
        End Select
    End Function

    Protected Function GetContainerText() As String
        Return String.Format("{0} x {1}", PageBO.Containers, CType(PageBO.ContainerType, EContainerType).ToString.Replace("_", " "))
    End Function

    Protected Function GetShippingTypeText() As String
        Return CType(PageBO.ShippingType, EShippingType).ToString.Replace("_", " ")
    End Function

    Protected Function GetPackingTypeText() As String
        Return CType(PageBO.PackingType, EPackingType).ToString.Replace("_", " ")
    End Function

    Protected Function GetContactName() As String
        If PageBO.CustomerID > 0 AndAlso PageBO.OrderByCustomerID > 0 Then
            Dim objCusContact As New CCustomerContact
            Try
                objCusContact.Load(PageBO.OrderByCustomerID)
            Catch ex As Exception
                Throw New ApplicationException("Can not load customer name", ex)
            End Try

            'check for data integrity
            If objCusContact.CustomerID <> PageBO.CustomerID Then
                Throw New ApplicationException("Order data invalid, please check for customer id and customer's contact id")
            End If

            Return objCusContact.ContactName
        Else
            Return Nothing
        End If
    End Function

    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try

            PageBO.Save()
            Response.Redirect(AppRoot() & Me.ORDER_LIST_URL)

        Catch ex As DataEntryException
            lblErr.Text = "Order number already exists. Please enter another one."
        Catch ex As Exception
            lblErr.Text = ex.Message
        End Try
    End Sub

End Class
